<template>
  <div class="background">
    <a-card style="margin-bottom: 10px">
      <a-row>
        <a-col :span="24">
          <a-form
            layout="inline"
            :form="form"
            @submit="handleSubmit"
          >
            <a-form-item label="openid">
              <a-input
                style="width:300px"
                allow-clear
                placeholder="请输入openid"
                v-model="openid"
              ></a-input>
            </a-form-item>
            <a-form-item label="选择时间">
              <a-range-picker
                @change="changeTime"
                :show-time="{
        defaultValue: [moment('00:00:00', 'HH:mm:ss'), moment('11:59:59', 'HH:mm:ss')],
      }"
                format="YYYY-MM-DD HH:mm:ss"
              />
            </a-form-item>

            <a-form-item>
              <a-button
                type="primary"
                html-type="submit"
              >搜索</a-button>
            </a-form-item>

          </a-form>

        </a-col>
      </a-row>
    </a-card>
    <a-card>
      <a-menu
        v-model="current"
        mode="horizontal"
        @click="handleClick"
      >
        <a-menu-item key="1">模板消息类 </a-menu-item>
        <!-- <a-menu-item key="2">关键字回复类 </a-menu-item>

        <a-menu-item key="3">其他类型 </a-menu-item> -->
      </a-menu>
      <div id="change">
        <a-table
          bordered
          :loading="loading"
          :pagination="pagination"
          :columns="columns"
          :data-source="dataSource"
          @change="handleTableChange"
          :scroll="{ x: true }"
        > <template
            slot="businessType"
            slot-scope="txt, record"
          >

            <span v-if="record.businessType == 1">消息群发</span>
            <span v-else>接口调用</span>
          </template>

          <span
            slot="status"
            slot-scope="txt, record"
          >
            <span v-if="record.status == 1">发送成功</span>
            <span v-else>已发送</span>

          </span>
          <template
            slot="action"
            slot-scope="txt, record"
          >

            <a
              class="mr-10"
              @click="toDetail(record)"
            >
              详情
            </a>

            <!-- <a
              class="mr-10"
              @click="deleteRecord(record)"
            >
              删除
            </a> -->
          </template>
        </a-table>
      </div>
      <!-- <create
        v-if="showAdd"
        :news="news"
        :appid="appid"
        @close="close"
      ></create> -->
      <detail
        v-if="showDetail"
        :detail="detail"
        @closeDetail="closeDetail"
      ></detail>
    </a-card>

  </div>
</template>

<script>
import { postAction, getAction } from '@/api/manage'
import detail from './detail.vue'
import moment from 'moment'
// import create from './create.vue'
const columns = [
  {
    title: '序号',
    width: 230,
    dataIndex: 'id'
  },
  {
    title: '发送openid',
    width: 300,
    dataIndex: 'openid'
  },
  // {
  //   title: '发送内容',
  //   dataIndex: 'content.data'
  // },
  {
    title: 'APPID',
    dataIndex:'appid'
  },
  {
    title: '发送状态',
    dataIndex: 'sendStatus'
    // scopedSlots: { customRender: 'status' }
  },

  {
    title: '创建时间',
    width: 200,
    dataIndex: 'createTime'
  },
  {
    title: '更新时间',
    width: 200,
    dataIndex: 'updateTime'
  },
  {
    title: '操作',
    width: 80,
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  components: {
    detail
  },
  props: {
    appid: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      columns,
      moment,
      current: ['1'],
      urlKey: 'basemgt-wx',
      dataSource: [],
      loading: false,
      startTime: '',
      endTime:'',
      openid: '',
      detail: {},
      showDetail: false,
      form: this.$form.createForm(this),
      showAdd: false,
      pagination: {
        total: 0,
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30', '40'],
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: total => `共 ${total} 条记录`,
        itemRender: (current, type, originalElement) => {
          if (type === 'prev') {
            return <a>上一页</a>
          } else if (type === 'next') {
            return <a>下一页</a>
          }
          return originalElement
        }
      }
    }
  },
  mounted() {
    this.loadData()
  },
  watch: {
    appid() {
      this.loadData()
    }
  },
  methods: {
    handleTableChange(pagination) {
      this.pagination = pagination
      this.loadData()
    },
    changeTime(date, dateString) {
      this.startTime = dateString[0]
      this.endTime = dateString[1]
    },
    close() {
      this.showAdd = false
      this.loadData()
    },
    add(record) {
      this.showAdd = true
      this.news = record
    },
    changeMenu() {
      this.loadData()
    },
    handleClick(e) {
      console.log('click', e.key)
      this.type = e.key
      this.pageNum = 1
      this.loadData(1)
    },
    toDetail(record) {
      this.detail = record
      this.showDetail = true
    },
    closeDetail() {
      this.showDetail = false
    },
    handleSubmit(e) {
      e.preventDefault()
      this.loadData(1)
    },
    loadData(key) {
      this.loading = true
      const params = {
        appid: this.appid,
        pageSize: this.pagination.pageSize,
        pageNum: this.pagination.current
      }
      if (key) {
        this.pagination.current = 1
        params.openid = this.openid
        params.pageNum = 1
        params.startTime = this.startTime
        params.endTime = this.endTime

      }
    
      postAction(`${this.urlKey}/WxTemplateMsgSendRecord/findWxTemplateMsgSendRecordListByCondition`, params).then(data => {
        if (data.code == 200) {
          this.dataSource = data.data.records
          // this.dataSource.map(item => {
          //   item.content = JSON.parse(item.content)
          // })
          this.pagination.total = data.data.total
        } else {
          this.$message.warning(data.msg || data.message)
        }
      })
      this.loading = false
    },
    deleteRecord(item) {
      this.$confirm({
        content: '确定要删除吗？',
        onOk: () => {
          const params = {
            id: item.id
          }
          getAction(`${this.urlKey}/WxTemplateTask/deleteWxTemplateTaskById`, params).then(data => {
            if (data.code == 200) {
              this.$message.success('删除成功')
              this.loadData()
            } else {
              this.$message.warning(data.msg || data.message)
            }
          })
        },
        onCancel() {}
      })
    }
  }
}
</script>
<style scoped>
.start::before {
  content: ' ';
  border: 4px solid red;
  border-radius: 4px;
  position: absolute;
  margin-left: -9px;
  margin-top: 6px;
}
.end::before {
  content: ' ';
  border: 4px solid green;
  border-radius: 4px;
  position: absolute;
  margin-left: -9px;
  margin-top: 6px;
}
</style>

<style>
#change .ant-table {
  font-size: 15px !important;
}
</style>
